var User = require("../model/user");
var Idalloc = require("../model/idalloc");
var crypto = require('crypto');


UserCtrl = {
    login: function *(){
        var userInfo = UserCtrl.getSession(this);
        var pageData = {
            userInfo: userInfo ? userInfo: {},
        }
        var assignData = {
            pageData: JSON.stringify(pageData)
        }
         yield this.render('user/login.jade', assignData);
    },
    logout: function *(){
        delete this.session.userInfo;
        this.redirect("/");
    },
    postLogin: function *(){
         yield this.render('user/login.jade');
    },
    postRegistery: function *(){
         yield this.render('user/login.jade');
    },
    profile: function *(){

        var user_info = null;
        var modelRes = null;
        // var promise = User.getUserInfo();

        // yield promise.then(function(res){
        //     modelRes = res;
        // });
        modelRes = this.session.userInfo;

        this.body = modelRes;
    },

    signUp: function *(){
        var retData  = {
            errno: 0,
            errmsg: '注册成功，请登录',
            data: {},
        };
        var user_info = null;
        var modelRes = null;
        var email = this.request.body.email;
        var password = this.request.body.password;
        if(email == "" || password == ""){
            retData.errno == 9;
            retData.errmsg = "用户名或密码为空，请重新输入";
            return this.body = retData;
        }

        var user_name = email.substr(email,email.indexOf("@"));

        var userId = 0;
        yield Idalloc.getId("idalloc_user").then(function(result){
            userId = result;;
            console.log(userId);
        }).catch(function(err){
            retData.errno = 9;
            retData.errmsg = "用户注册失败，请稍后再试";
        });
        if(retData.errno == 9){
            return this.body = retData;
        }


        var md5sum = crypto.createHash('md5');
        md5sum.update(password);
        password = md5sum.digest('hex');

        var queryData = {
            user_id: userId,
            user_name: user_name,
            email: email,
            password: password,
        }

        yield User.insert(queryData).then(function(res){
            modelRes = res;
        }).catch(function(err) {
            retData.errno = 9;
            retData.errmsg = "用户注册失败，请稍后再试";
        });

        if(retData.errno == 9){
            return this.body = retData;
        }
        
        this.body = retData;
    },

    signIn: function *(){
        var retData  = {
            errno: 0,
            errmsg: '',
            data: {},
        };
        var user_info = null;
        var modelRes = null;
        var email = this.request.body.email;
        var password = this.request.body.password;
        if(email == "" || password == ""){
            retData.errno == 9;
            retData.errmsg = "用户名或密码为空，请重新输入";
            return this.body = retData;
        }
        var queryData = {
            email: email,
        }
        yield User.getUserInfoByPrimaryKey(queryData).then(function(res){
            modelRes = res;
        });

        var md5sum = crypto.createHash('md5');
        md5sum.update(password);
        password = md5sum.digest('hex');
        if(!modelRes){
            retData.errno = 3;
            retData.errmsg = "用户未注册";
            return this.body = retData;
        }
        console.log(password, modelRes);
        if(password != modelRes.password){
            retData.errno = 2;
            retData.errmsg = "用户名或密码错误";
            return this.body = retData;
        }
        console.log(this.session);
        this.session.userInfo = modelRes;
        delete this.session.userInfo['password'];
        
        this.body = retData;
    },

    getSession: function(context){
        //console.log(context.session);
        if(context.session && context.session.userInfo){
            return context.session.userInfo;
        } 
        else 
        { 
            return false;
        }
    },

    

}
module.exports = UserCtrl;
